using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SecureSession ss = new SecureSession();
        ss.isActive();
        this.dlSex.DataTextField = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        IDbConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString);
        string sql_script =
    @"
    INSERT INTO [Participants](occupation, nationality,birthday,email, gender) VALUES (@occupation,@nationality,@birthday,@email,@gender); SELECT @@IDENTITY
    ";
        using (conn)
        {
            conn.Open();
            IDbTransaction tran = conn.BeginTransaction();
            try
            {
                IDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql_script;
                IDbDataParameter param = cmd.CreateParameter();
                param.ParameterName = "@occupation";
                param.DbType = DbType.String;
                param.Value = this.txtOccupation.Text.Trim();
                cmd.Parameters.Add(param);

                param = cmd.CreateParameter();
                param.ParameterName = "@birthday";
                param.DbType = DbType.String;
                param.Value = this.txtDay.Text.ToString().Trim() + "-" + this.txtMonth.Text.ToString().Trim() + "-" + this.txtYear.Text.ToString().Trim();
                cmd.Parameters.Add(param);


                param = cmd.CreateParameter();
                param.ParameterName = "@nationality";
                param.DbType = DbType.String;
                param.Value = this.txtNationality.Text.Trim();
                cmd.Parameters.Add(param);

                param = cmd.CreateParameter();
                param.ParameterName = "@email";
                param.DbType = DbType.String;
                param.Value = this.txtEmail.Text.Trim();
                cmd.Parameters.Add(param);

                param = cmd.CreateParameter();
                param.ParameterName = "@gender";
                param.DbType = DbType.String;
                param.Value = this.dlSex.SelectedValue;
                cmd.Parameters.Add(param);

                cmd.Transaction = tran;
                int new_pant_id = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                tran.Commit();
                Session[FormContract.USER_REF] = new Participant(new_pant_id, this.txtEmail.Text.Trim());                
                Response.Redirect("Survey.aspx", false);

            }
            catch (Exception error)
            {
                tran.Rollback();
                throw (error);
            }
        }
    }
}
